#include <QtGui>

#include "src/color.h"
#include "src/particle.h"
#include "particlesDialog.h"
#include "fabricTreeItem.h"
#include "particleTreeItem.h"
#include "treeItem.h"

ParticlesDialog::ParticlesDialog(TreeItem *_item, QWidget *_parent)
    : QDialog(_parent), item(_item){

    setWindowTitle(QString::fromUtf8("Modificando todas as partículas"));

    colorR = 255;
    colorG = 255;
    colorB = 255;
    colorAlpha = 255;
    mass = 0.0001;
    kdAir = 0.3;
    miC = 0.01;
//    configuration = 0;

    createLayout();
}

ParticlesDialog::~ParticlesDialog(){
}

void ParticlesDialog::createLayout(){
    //--------------------------------------Mass
    QDoubleSpinBox *massDoubleSpinBox = new QDoubleSpinBox();
    massDoubleSpinBox->setRange(0.00001, 10.0);
    massDoubleSpinBox->setSingleStep(1.0);
    massDoubleSpinBox->setDecimals(5);
    massDoubleSpinBox->setValue(mass);
    connect(massDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeMass(double)));

    QHBoxLayout *massLayout = new QHBoxLayout;
    massLayout->addWidget(massDoubleSpinBox);
    massLayout->addStretch(1);

    massGroupBox = new QGroupBox(QString::fromUtf8("Massa"));
    massGroupBox->setCheckable(true);
    massGroupBox->setChecked(false);
    massGroupBox->setLayout(massLayout);


    //--------------------------------------Kd de resistencia do ar
    QDoubleSpinBox *kdAirDoubleSpinBox = new QDoubleSpinBox();
    kdAirDoubleSpinBox->setRange(0.0, 10.0);
    kdAirDoubleSpinBox->setSingleStep(0.1);
    kdAirDoubleSpinBox->setDecimals(5);
    kdAirDoubleSpinBox->setValue(kdAir);
    connect(kdAirDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeKdAir(double)));

    QHBoxLayout *kdAirLayout = new QHBoxLayout;
    kdAirLayout->addWidget(kdAirDoubleSpinBox);
    kdAirLayout->addStretch(1);

    kdAirGroupBox = new QGroupBox(QString::fromUtf8("Kd central (resistencia do ar)"));
    kdAirGroupBox->setCheckable(true);
    kdAirGroupBox->setChecked(false);
    kdAirGroupBox->setLayout(kdAirLayout);


    //--------------------------------------MiC
    QDoubleSpinBox *miCDoubleSpinBox = new QDoubleSpinBox();
    miCDoubleSpinBox->setRange(0.0, 10.0);
    miCDoubleSpinBox->setSingleStep(1.0);
    miCDoubleSpinBox->setDecimals(5);
    miCDoubleSpinBox->setValue(miC);
    connect(miCDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeMiC(double)));

    QHBoxLayout *miCLayout = new QHBoxLayout;
    miCLayout->addWidget(miCDoubleSpinBox);
    miCLayout->addStretch(1);

    miCGroupBox = new QGroupBox(QString::fromUtf8("Atrito"));
    miCGroupBox->setCheckable(true);
    miCGroupBox->setChecked(false);
    miCGroupBox->setLayout(miCLayout);


    //--------------------------------------Cor
    colorPushButton = new QPushButton(tr("Cor"));
    colorPushButton->setEnabled(false);
    connect(colorPushButton, SIGNAL(clicked()), this, SLOT(changeColor()));
    setButtonColor();

    QHBoxLayout *colorLayout = new QHBoxLayout;
    colorLayout->addWidget(colorPushButton);

    colorGroupBox = new QGroupBox(QString::fromUtf8("Cor das partículas"));
    colorGroupBox->setCheckable(true);
    colorGroupBox->setChecked(false);
    colorGroupBox->setLayout(colorLayout);
    connect(colorGroupBox, SIGNAL(toggled(bool)), colorPushButton, SLOT(setEnabled(bool)));


    //--------------------------------------Botões
    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    connect(buttonBox, SIGNAL(accepted()), this, SLOT(acceptChange()));
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    buttonBox->setLocale(QLocale(QLocale::Portuguese, QLocale::Brazil));


    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(massGroupBox);
    mainLayout->addWidget(kdAirGroupBox);
    mainLayout->addWidget(miCGroupBox);
    mainLayout->addWidget(colorGroupBox);
    mainLayout->addWidget(buttonBox);
    mainLayout->addStretch(1);

    setLayout(mainLayout);
}

void ParticlesDialog::setButtonColor(){
    //    QString colorStr = QString::fromUtf8("color: rgb(255, 255, 255);\n" "background-color: rgb(85, 0, 0, 255);");
    QString colorStr;
    if(colorR + colorB + colorG + colorAlpha < 600){
        colorStr = QString::fromUtf8("color: rgb(255, 255, 255);\n");
    }else{
        colorStr = QString::fromUtf8("color: rgb(0, 0, 0);\n");
    }
    colorStr.append(QString::fromUtf8("background-color: rgb("));
    colorStr.append(QString::number(colorR));
    colorStr.append(QString::fromUtf8(", "));
    colorStr.append(QString::number(colorG));
    colorStr.append(QString::fromUtf8(", "));
    colorStr.append(QString::number(colorB));
    colorStr.append(QString::fromUtf8(", "));
    colorStr.append(QString::number(colorAlpha));
    colorStr.append(QString::fromUtf8(");"));

    colorPushButton->setStyleSheet(colorStr);
}

void ParticlesDialog::acceptChange(){
    bool changeColor = colorGroupBox->isChecked();
    bool changeMass = massGroupBox->isChecked();
    bool changeKdAir = kdAirGroupBox->isChecked();
    bool changeMiC = miCGroupBox->isChecked();

    if(changeKdAir)
    {
        Fabric *fabric = ((FabricTreeItem *)(((QTreeWidgetItem *)item)->parent()))->getFabric();
        fabric->recalculateParticlesKd(kdAir);
    }

    int size = ((QTreeWidgetItem *)item)->childCount();
    for(int i = 0; i < size; i++)
    {
        Particle *particle = ((ParticleTreeItem *)item->child(i))->getParticle();
        if(changeColor) particle->setColor(Color(colorR / 255.0, colorG / 255.0, colorB / 255.0, colorAlpha / 255.0));
        if(changeMass) particle->setMass(mass);
        if(changeMiC) particle->setMiC(miC);
    }

    emit done();

    accept();
}

void ParticlesDialog::changeColor(){
    QColor color = QColorDialog::getColor(QColor(colorR, colorG, colorB, colorAlpha), this, "Escolha a cor", QColorDialog::ShowAlphaChannel);
    if (color.isValid()){
        colorR = color.red();
        colorG = color.green();
        colorB = color.blue();
        colorAlpha = color.alpha();
    }
    setButtonColor();
}

void ParticlesDialog::changeMass(double _m){
    mass = _m;
}

void ParticlesDialog::changeKdAir(double _kd){
    kdAir = _kd;
}

void ParticlesDialog::changeMiC(double _miC){
    miC = _miC;
}

//void ParticlesDialog::changeConfiguration(int _conf){
//    configuration = _conf;
//}
